'''
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:123456@localhost:3306/flask'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
'''
import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


def get_db_url(dbinfo):
    engine = dbinfo.get("ENGINE") or "sqlite"
    driver = dbinfo.get("DRIVER") or "sqlite"
    user = dbinfo.get("USER") or ""
    password = dbinfo.get("PASSWORD") or ""
    host = dbinfo.get("HOST") or ""
    port = dbinfo.get("PORT") or ""
    name = dbinfo.get("NAME") or ""
    return '{}+{}://{}:{}@{}:{}/{}'.format(engine, driver, user, password, host, port, name)


class Config:
    DEBUG = False
    TESTING = False
    SQLALCHEMY_TRACK_MODIFICATIONS = False

    # 配置session
    SECRET_KEY = 'kene',
    SESSION_TYPE = 'redis'
    PERMANENT_SESSION_LIFETIME = 60 * 60 * 24 * 30

    # 缓存
    CACHE_TYPE = 'redis'
    CACHE_DEFAULT_TIMEOUT = 60 * 60 * 24 * 30  # 5分钟
    CACHE_REDIS_HOST = 'localhost'
    CACHE_REDIS_PORT = 6379
    CACHE_REDIS_DB = 0

    # 配置邮箱
    MAIL_SERVER = 'smtp.163.com'
    MAIL_PORT = 25
    MAIL_USERNAME = '18036311234@163.com'
    MAIL_PASSWORD = 'Yaohan520'
    MAIL_DEFAULT_SENDER = MAIL_USERNAME


# 开发环境
class DevelopConfig(Config):
    DEBUG = True
    # 默认数据库
    defaultdatabase = {
        "ENGINE": "mysql",
        "DRIVER": "mysqlconnector",
        "USER": "root",
        "PASSWORD": "123456",
        "HOST": "localhost",
        "PORT": "3306",
        "NAME": "yjh_movie",
    }
    SQLALCHEMY_DATABASE_URI = get_db_url(defaultdatabase)

    # 所有client注册的使用的用户
    userdatabase = {
        "ENGINE": "mysql",
        "DRIVER": "mysqlconnector",
        "USER": "root",
        "PASSWORD": "123456",
        "HOST": "localhost",
        "PORT": "3306",
        "NAME": "yjh_users",
    }
    SQLALCHEMY_BINDS = {
        'yjh_users': get_db_url(userdatabase)
    }
    SQLALCHEMY_BINDS = SQLALCHEMY_BINDS
    SQLALCHEMY_TRACK_MODIFICATIONS = True


# 测试环境
class TestConfig(Config):
    TESTING = True
    dbinfo = {
        "ENGINE": "mysql",
        "DRIVER": "mysqlconnector",
        "USER": "root",
        "PASSWORD": "123456",
        "HOST": "",
        "PORT": "",
        "NAME": "flask",
    }
    SQLALCHEMY_DATABASE_URI = get_db_url(dbinfo)


# 演示环境
class StagingConfig(Config):
    dbinfo = {
        "ENGINE": "mysql",
        "DRIVER": "mysqlconnector",
        "USER": "root",
        "PASSWORD": "123456",
        "HOST": "",
        "PORT": "",
        "NAME": "flask",
    }
    SQLALCHEMY_DATABASE_URI = get_db_url(dbinfo)


# 线上环境
class ProductConfig(Config):
    dbinfo = {
        "ENGINE": "mysql",
        "DRIVER": "mysqlconnector",
        "USER": "root",
        "PASSWORD": "123456",
        "HOST": "",
        "PORT": "",
        "NAME": "flask",
    }
    SQLALCHEMY_DATABASE_URI = get_db_url(dbinfo)


envs = {
    "develop": DevelopConfig,
    "testing": TestConfig,
    "staging": StagingConfig,
    "product": ProductConfig,
    "default": DevelopConfig,
}

# 头像上传
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'JPG', 'PNG', 'bmp'])  # 允许上传图片的格式
UPLOAD_DIR = os.path.join(BASE_DIR, 'App/static/upload/icons/')  # 文件保存在这个文件夹中
FILE_PATH = '/static/upload/icons/'  # 数据库保存这个路径
